﻿
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine.UI;
using VFramework;

public class AppNet : HttpClientXml 
{

	public static readonly string GAME_ID = "2";
	public static readonly string PLAY_STYLE_MAIN = "0";
	public static readonly string PLAY_STYLE_SUB = "1";

	public static readonly int ALL_RECORD = 0;
	public static readonly int PRIZE_INDEX = 1;

	public static string netWinContent = ""; //中奖内容
	public static int netWinLvl = 0; //中奖类型
	public static int netWinMoney = 0; //中奖的钱

	public static decimal balance = 0; //服务器每次的带回来的余额

	public static List<int> recordBeginIndex = new List<int>{0,0};

	/// <summary>
	/// Js the sync time.
	/// </summary>
	/// <param name="callback">Callback.</param>
	public void jSyncTime(Action<HttpRsp> callback)
	{
		Dictionary<string,string> data = new Dictionary<string, string> ();
		AddRequest (RequestType.GET, new HttpRequest (NetConfig.HOST_URL, data, callback));
	}

	/// <summary>
	/// 登录接口
	/// </summary>
	/// <param name="loginstr">账号</param>
	/// <param name="pwd">明文密码</param>
	/// <param name="callback">回调</param>
	public void Login(string loginstr, string pwd, Action<HttpRsp> callback)
	{
		Dictionary<string,string> data = new Dictionary<string, string> () {
			{ "loginType","3" },
			{ "loginStr", loginstr },
			{ "pwd", HttpClientXml.MD5(pwd) },
		};
		AddRequest (RequestType.POST, new HttpRequest (NetConfig.HOST_URL, "121101", data, delegate(HttpRsp obj) {
			if(obj.backCode == 0){
				HttpClientXml.ssId = obj.body.SelectSingleNode("ssId").InnerText.Trim();
				HttpClientXml.userID = obj.content.SelectSingleNode("userId").InnerText.Trim();
				balance = decimal.Parse(obj.content.SelectSingleNode("accountMoney").InnerText.Trim());
			}
			callback(obj);
		}));
	}
	/// <summary>
	/// 下注接口
	/// </summary>
	/// <param name="childId">玩法编号,主玩法0，子玩法1</param>
	/// <param name="serial">序号，主玩法传1，子玩法每玩一次就加1，从1开始</param>
	/// <param name="content">下注内容</param>
	/// <param name="cb">回调</param>
	/// <param name="cleanTicket">是否清空ticket,默认不清空,每次玩主玩法都清空。</param>
	public void makeBet(string childId,int serial,string content ,Action<HttpRsp> cb,bool cleanTicket = false){
		if (cleanTicket == true)
			HttpClientXml.ticket = "";
		
		Dictionary<string,string> data = new Dictionary<string, string> () {
			{ "userId",HttpClientXml.userID },
			{ "gameId", GAME_ID },
			{ "childId", childId },
			{"ticket", HttpClientXml.ticket},
			{"betContent","<serial>"+serial+"</serial><content>"+content+"</content>"},
		};

		AddRequest(RequestType.POST,new HttpRequest(NetConfig.HOST_URL,"130301",data,delegate(HttpRsp obj) {
			if(obj.backCode == 0){
				HttpClientXml.ticket = obj.content.SelectSingleNode("ticket").InnerText;

				netWinContent = obj.content.SelectSingleNode("winContent").InnerText;
				netWinLvl = int.Parse(obj.content.SelectSingleNode("winLevel").InnerText);
				netWinMoney = (int)(decimal.Parse(obj.content.SelectSingleNode("winMoney").InnerText));
				try{
					balance = (int)decimal.Parse(obj.content.SelectSingleNode("balance").InnerText.Trim());
				}catch(Exception e){
					VLogger.error(e.ToString());
				}
			}
			cb(obj);
		}));
	}


	/// <summary>
	/// 兑奖
	/// </summary>
	/// <param name="cb">callback</param>
	public void makeCash(Action<HttpRsp> cb){
		Dictionary<string,string> data = new Dictionary<string, string> () {
			{ "userId",HttpClientXml.userID },
			{ "gameId", GAME_ID },
			{"ticket", HttpClientXml.ticket},
		};

		AddRequest(RequestType.POST,new HttpRequest(NetConfig.HOST_URL,"130401",data,delegate(HttpRsp rsp) {
			if(rsp.backCode == 0)
				balance = decimal.Parse(rsp.content.SelectSingleNode("balance").InnerText.Trim());
			cb(rsp);
		}));
	}


	/// <summary>
	/// 获取投注记录
	/// </summary>
	/// <param name="begIndex">开始索引</param>
	/// <param name="type">类型，中奖和全部</param>
	/// <param name="maxCount">数量</param>
	/// <param name="cb">回调</param>
	public void betRecord(int begIndex, int type, int maxCount , Action<HttpRsp> cb){

		Dictionary<string,string> data = new Dictionary<string, string> () {
			{ "userId",HttpClientXml.userID },
			{ "gameId", GAME_ID },
			{ "type", type+""},
			{ "begIndex",begIndex+""},
			{ "maxCount",maxCount+""},
		};

		AddRequest(RequestType.POST,new HttpRequest(NetConfig.HOST_URL,"131001",data,delegate(HttpRsp rsp) {
			cb(rsp);
		}));
	}

	public void getAccountMoney(Action<HttpRsp> cb){
		Dictionary<string,string> data = new Dictionary<string, string> () {
			{ "userId",HttpClientXml.userID },
		};

		AddRequest(RequestType.POST,new HttpRequest(NetConfig.HOST_URL,"120305",data,delegate(HttpRsp rsp) {
			cb(rsp);
		}));
	}
}